package com.bianguo.android.apushdemo.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.entity.Images;
import com.bianguo.android.apushdemo.entity.Subject;
import com.bianguo.android.apushdemo.utils.GlideUtil;
import com.bianguo.android.apushdemo.utils.MyLog;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/7/31.
 */

public class MovieAdapter extends BaseRecyclerAdapter<Subject> {

    public MovieAdapter(Context mContext, List<Subject> list) {
        super(mContext, list);
    }

    @Override
    public BaseViewHolder createHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (0 == viewType) {
            view = inflater.inflate(R.layout.head_movie_layout, parent, false);
            return new HeadHolder(view);
        } else if (1 == viewType) {
            view = inflater.inflate(R.layout.item_movie_select_layout, parent, false);
            return new SelectHolder(view);
        } else {
            view = inflater.inflate(R.layout.item_movie_layout, parent, false);
            return new ViewHolder(view);
        }
    }

    @Override
    public void bindHolder(BaseViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                break;
            case 1:
                final SelectHolder selectHolder = (SelectHolder) holder;
                selectHolder.nearbyTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickItemListener != null) {
                            onClickItemListener.onClickItem(v, selectHolder.getAdapterPosition());
                        }
                    }
                });
                selectHolder.timeTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickItemListener != null) {
                            onClickItemListener.onClickItem(v, selectHolder.getAdapterPosition());
                        }
                    }
                });
                selectHolder.typeTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickItemListener != null) {
                            onClickItemListener.onClickItem(v, selectHolder.getAdapterPosition());
                        }
                    }
                });
                selectHolder.personTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickItemListener != null) {
                            onClickItemListener.onClickItem(v, selectHolder.getAdapterPosition());
                        }
                    }
                });
                break;
            case 2:
                final ViewHolder viewHolder = (ViewHolder) holder;
                Subject subject = list.get(position);
                viewHolder.titleTv.setText(subject.getTitle());
                viewHolder.contentTv.setText(subject.getRating().toString());
                Images images = subject.getImages();
                if (images != null) {
                    if (!images.getLarge().equals(viewHolder.bannerImage.getTag(R.id.id_movie_image_url))) {
                        String large = images.getLarge();
                        GlideUtil.loadImage(mContext, viewHolder.bannerImage, large, 0);
                        viewHolder.bannerImage.setTag(R.id.id_movie_image_url, large);
                    }
                    if (!images.getLarge().equals(viewHolder.iconImage.getTag(R.id.id_movie_image_url))) {
                        String large = images.getLarge();
                        GlideUtil.loadImage(mContext, viewHolder.iconImage, large, 48);
                        viewHolder.iconImage.setTag(R.id.id_movie_image_url, large);
                    }
                    viewHolder.bannerImage.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (onClickItemListener != null) {
                                onClickItemListener.onClickItem(viewHolder.bannerImage, viewHolder.getAdapterPosition());
                            }
                        }
                    });
                }
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (0 == position) {
            return 0;
        } else if (1 == position) {
            return 1;
        } else {
            return 2;
        }
    }

    public static class ViewHolder extends BaseViewHolder {

        @BindView(R.id.item_title)
        TextView titleTv;
        @BindView(R.id.item_content)
        TextView contentTv;
        @BindView(R.id.item_icon)
        ImageView iconImage;
        @BindView(R.id.item_image)
        ImageView bannerImage;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static class HeadHolder extends BaseViewHolder {

        public HeadHolder(View itemView) {
            super(itemView);
        }
    }

    public static class SelectHolder extends BaseViewHolder {

        @BindView(R.id.item_select_nearby)
        TextView nearbyTv;
        @BindView(R.id.item_select_time)
        TextView timeTv;
        @BindView(R.id.item_select_type)
        TextView typeTv;
        @BindView(R.id.item_select_person)
        TextView personTv;

        public SelectHolder(View itemView) {
            super(itemView);
        }
    }
}
